package com.ydche.dao.impl;

import com.ydche.client.DaoClient;
import com.ydche.dao.IInsuranceDao;
import com.ydche.idl_output.*;
import com.ydche.idl_output.InsuranceTypes;
import com.ydche.idl_output.InsuranceUnit;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TProtocol;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

public class InsuranceDaoImpl implements IInsuranceDao {

    private static List<InsuranceTypes> insuranceTypesList = null;

    private static List<InsuranceUnitInfo> insuranceUnitInfoList = null;

    @Autowired
    private DaoClient daoClient;

    @Override
    public List<InsuranceTypes> getInsuranceTypes() throws TException{
        if(insuranceTypesList == null){
            TProtocol protocol =  daoClient.getConnection();
            IDao.Iface client = new IDao.Client(protocol);
             try {
                insuranceTypesList = client.get_insurance_types();
             }finally {
                daoClient.returnConnection(protocol);
            }
        }
        return insuranceTypesList;
    }

    @Override
    public List<InsuranceUnitInfo> getInsuranceUnitList() throws TException {
        if (insuranceUnitInfoList == null) {
            TProtocol protocol = daoClient.getConnection();
            IDao.Iface client = new IDao.Client(protocol);
            try {
                insuranceUnitInfoList = client.get_insurance_unit_list();
            } finally {
                daoClient.returnConnection(protocol);
            }
        }
        return insuranceUnitInfoList;
     }
}
